package wps.component.elementsetting {
import mx.binding.utils.BindingUtils;
import mx.events.FlexEvent;

import wps.util.EnableChecker;

/**
 * 设置属性时用的输入框
 */
public class SettingTextInput extends SettingTextInputBase implements ISettingItem {

    //--------------------------------------------------------------------------
    //
    // Properties
    //
    //--------------------------------------------------------------------------

    private var _target:Object;

    private var _targetProperty:String;

    private var _targetStyle:String;

    private var _needCopy:Boolean = false;

	//--------------------------------------------------------------------------
    //
    // Methods
    //
    //--------------------------------------------------------------------------

    public function get needCopy():Boolean {
        return _needCopy;
    }

    public function set needCopy(needCopy:Boolean):void {
        _needCopy = needCopy;
    }

    [Bindable]
    public function get target():Object {
        return _target;
    }

    public function set target(target:Object):void {
        _target = target;
        targetChanged();
    }

    public function get targetProperty():String {
        return _targetProperty;
    }

    public function set targetProperty(targetProperty:String):void {
        _targetProperty = targetProperty;
        targetPropertyChanged();
    }

    public function get targetStyle():String {
        return _targetStyle;
    }

    public function set targetStyle(targetStyle:String):void {
        _targetStyle = targetStyle;
        targetStyleChanged();
    }

    public function checkEnable():void {
        var checker:EnableChecker = new EnableChecker(this);
        enabled = checker.getEnableValue();
    }

    private function targetChanged():void {
        checkEnable();
        targetPropertyChanged();
        targetStyleChanged();
    }

    private function targetPropertyChanged():void {
        if (targetProperty) {
            BindingUtils.bindSetter(textSetter, target, targetProperty);
        }
    }

    protected function textSetter(value:Object):void {
        if (target && targetProperty) {
            text = target[targetProperty];
        } else {
            text = "";
        }
    }

    private function targetStyleChanged():void {
        // 当样式对目标有效时才显示值
        if (target) {
            // 只有是用来设置样式的输入框时才读取样式，否则会覆盖targetProperty的值
            if (targetStyle && enabled) {
                text = target.getStyle(targetStyle);
            }
        } else {
            text = "";
        }
    }

    override protected function inputCompleted():void {
        if (target) {
            if (targetProperty) {
                target[targetProperty] = text;
            } else if (targetStyle) {
                target.setStyle(targetStyle, text);
            }
        }
    }
	
	//--------------------------------------------------------------------------
	//
	// Event Handlers
	//
	//--------------------------------------------------------------------------
	
	override protected function initializeHandler(e:FlexEvent):void {
		super.initializeHandler(e);
		
		enabled = target ? true : false;
	}

}
}
